$(function () {
    // 倒计时
    var times = 1 * 60 * 60 * 1000 - 100;
    function conutDown() {
        m = parseInt(times / (1000 * 60));    //计算分
        m = m < 10 ? "0" + m : m;  //三维运算符转换格式
        s = parseInt(times % (1000 * 60) / 1000);   // 计算选秒
        s = s < 10 ? "0" + s : s;  //三维运算符转换格式
        $("#time").text("00");
        $("#min").text(m);
        $("#sec").text(s)
        times -= 1000;
    }
    setInterval(() => {

        conutDown()

    }, 1000)

    // 点击第几道题
    let index = 1;
    $("#box7").on("click", "a", function () {
        if (!$(this).children().hasClass("photo")) {
            $(".judge").text("");
            $(".dui").text("");
            $(".jiexi").text("");
        }
        $(".topic").eq($(this).index()).show().siblings(".topic").hide(); //当前标签添加背景图片
        $(this).children().addClass("photo").text(""); // 其他兄弟元素去掉背景图和文本

        index = $(this).index() + 1;
        $(".sum_1").text("第" + index + "题共/7道");
        let num = 7 - $(".photo").length;

        $("#span_8").text("未答" + num + "题");
    });




    // 点击下一道题
    $("#box3").on("click", ".btn_1", function () {
        if (index < 7) {
            $(".judge").text("");
            $(".dui").text("");
            $(".jiexi").text("");
        }


        $(this).parents(".topic").next(".topic").show().siblings(".topic").hide();


        let sum = 7 - $(".photo").length - 1;
        if (sum < 0 || sum == 7) {
            return
        }

        let num = index + 1
        $("#box7 span").eq(index).addClass("photo").text("");
        if (index <= 6) {
            $(".sum_1").text("第" + num + "题共/7道");
            $("#span_8").text("未答" + sum + "题");
            index++
        }
    });







    //获取数据
    let sum = 0;
    function find() {
        $.get("http://localhost:3000/find/list", (res) => {
            res.forEach((eitm, index) => {
                let num_7 = index + 1;
                let uls = `<ul class="topic" >
                <li id="span_5">
                    <span>单选题</span> <span class="sum_1">第1题/共7道</span>
                </li>
                <li>${num_7}.${eitm.analysis} </li>
                <br>
                <li><span class="judge_1">A</span>${eitm.options[0]}</li>
                <li><span class="judge_1">B</span>${eitm.options[1]}</li>
                <li><span class="judge_1">C</span>${eitm.options[2]}</li>
                <li><span class="judge_1">D</span>${eitm.options[3]}</li>
                <div id="box4">
                    <ul>
                        <li class="judge"></li>
                        <li class="dui"></li>
                        <li class="jiexi"></li>
                    </ul>
                    <button class="btn_1">下一题</button>
                </div>
              </ul>`
                $("#box3").append(uls);
                let spans = ` <a href="javascript:;"><span>${num_7}</span></a>`;
                $("#box7").append(spans);
            });
            $("#box7 span").eq(0).addClass("photo").text("");
        })
    }
    find();


    let arr2 = [];
    let arr = ["A", "B", "C", "D"];
    $("#box3").on("click", ".judge_1", function () {
        if ($(".judge").text() == "") {
            var str = $(this).text();
            $.get("http://localhost:3000/result/list", res => {
                let num3 = null;
                // 获取所选的文本值并判断
                if (str == "A") {
                    num3 = 0;
                } else if (str == "B") {
                    num3 = 1;
                } else if (str == "C") {
                    num3 = 2;
                } else if (str == "D") {
                    num3 = 3;
                }
                // let data = 0;

                let num4 = res[$(this).parents(".topic").index()].answer; //正确答案
                // 判断是否正确
                if (num3 == num4) {
                    // 正确           
                    $(this).css({
                        backgroundColor: "green"
                    })
                    $(".judge").css("color", "green");
                    $(".judge").text("考生答案:" + str + "√");
                    $(".dui").text("正确答案:" + arr[num4]);
                    $(".jiexi").text("解析:" + res[$(this).parents(".topic").index()].analysis);
                    sum++; //计数
                } else {
                    // 错误
                    $(this).css({
                        backgroundColor: "red"
                    })
                    $(".judge").css("color", "red");
                    $(".judge").text("考生答案:" + str + "×");
                    $(".dui").text("正确答案:" + arr[num4]);
                    $(".jiexi").text("解析:" + res[$(this).parents(".topic").index()].analysis);
                    let data = res[$(this).parents(".topic").index()];
                    arr2.push(data);
                }

                localStorage.setItem("sum1", sum);
                localStorage.setItem("errors", JSON.stringify(arr2));
            })
        }

    });

    // //交卷
    $("#jiaojuan").click(function () {
        $.get("http://localhost:3000/err/list", { arr2 }, (req, res) => {



        })
        location.href = "./result.html";
    })

})